import $ from 'jquery';
import Config from '../config/Config';

const getLikeInfo = function(user) {
  let APIUrl = Config.getAPIUrl('get_like_num');
  let promise = new Promise(function (resolve, reject) {
    $.ajax({
      url: APIUrl, // 授权登录
      type: 'GET',
      cache: false,
      contentType: 'application/json;charset=utf-8',
      dataType: 'json',
      headers: {
        Accept: 'application/json'
      },
      beforeSend: (request) => {
        request.setRequestHeader('X-iChangTou-Json-Api-Token', Config.getApiToken());
        request.setRequestHeader('X-iChangTou-Json-Api-User', user.userId);
      },
      success: resolve,
      error: reject
    });
  })
  return promise;
}

const like = function ({data, user}) {
  let APIUrl = Config.getAPIUrl('post_like');
  APIUrl = APIUrl.replace(':sentenceId', data.sentenceId).replace(':groupId', data.groupId)
    .replace(':playerId', data.playerId)

  let promise = new Promise(function (resolve, reject) {
    $.ajax({
      url: APIUrl, // 授权登录
      type: 'PUT',
      cache: false,
      contentType: 'application/json;charset=utf-8',
      dataType: 'json',
      data: JSON.stringify(data),
      headers: {
        Accept: 'application/json'
      },
      beforeSend: (request) => {
        request.setRequestHeader('X-iChangTou-Json-Api-Token', Config.getApiToken());
        request.setRequestHeader('X-iChangTou-Json-Api-User', user.userId);
      },
      success: resolve,
      error: reject
    });
  })
  return promise
}

export {
  getLikeInfo,
  like
}
